(function () {
  $(function () {
    // 将金额类型转为数字类型  
    var toNum = function (str) {
      return str.replace(/\,|\￥/g, "");
    }

    // 保留两位小数
    var toPrice = function (num) {
      num = parseFloat(toNum(num).replace(/(\.\d{2})\d+$/, "$1")).toFixed(2).toString().split(".");
      num[0] = num[0].replace(new RegExp('(\\d)(?=(\\d{3})+$)', 'ig'), "$1,");
      return num.join(".");;
    }




    var Chart = function () {
      var myChart = echarts.init(document.getElementById('detail'));
      var dom = {
        template: $('#tab-template'),
        tab1: $('#tab-detail-1'),
        tab2: $('#tab-detail-2')
      }
      var option = {
        // 图表配置
        color: ['#3398DB', '#233333', '#F4A460'],
        legend: {
          data: ['资金总量', '资产总量', '适配总量']
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {            // 坐标轴指示器，坐标轴触发有效
            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: ['累积总量', '今年新增'],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: []
      };
      return {
        renderChart: function () {
          // 渲染图表
          myChart.setOption(option)
        },
        setDate: function (all, currentYear) {
          // all 累计总量 currentYear 今年新增
          var op = [{
            name: '资金总量',
            type: 'bar',
            barWidth: '10%',
            data: [all.totalMoney, currentYear.totalMoney]
          }, {
            name: '资产总量',
            type: 'bar',
            barWidth: '10%',
            data: [all.totalAssets, currentYear.totalAssets]
          }, {
            name: '适配总量',
            type: 'bar',
            barWidth: '10%',
            data: [all.totalAdaptation, currentYear.totalAdaptation]
          }]
          option.series = op
        },
        getData: function () {
          var _this = this
          $.ajax({
            url: '../../../chart.json',
            type: 'GET',
            success: function (res) {
              console.log(res)
              // 渲染列表
              _this.setDate(res.data.all, res.data.currentYear)
              _this.renderChart()
              _this.renderTabs(res.data.all, res.data.currentYear)
            }
          })
        },
        renderTabs: function (all, currentYear) {
          all.totalMoney = toPrice(all.totalMoney)
          all.totalAssets = toPrice(all.totalAssets)
          all.totalAdaptation = toPrice(all.totalAdaptation)
          currentYear.totalMoney = toPrice(currentYear.totalMoney)
          currentYear.totalAssets = toPrice(currentYear.totalAssets)
          currentYear.totalAdaptation = toPrice(currentYear.totalAdaptation)
          this.renderTab(dom.tab1, all)
          this.renderTab(dom.tab2, currentYear)
        },
        renderTab: function (target, data) {
          var resource = dom.template.html()
          var html = template.render(resource, data);
          target.html(html)
        },
        init: function () {
          this.getData()
        }
      }
    }
    var chart = new Chart()
    chart.init()
  })
})()